﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace Rainy.FrameWork
{
    /// <summary>
    /// 显示类
    /// </summary>
    public static class MessageBox
    {
        /// <summary>
        /// 显示消息
        /// </summary>
        /// <param name="control">要注册到的控件</param>
        /// <param name="message">需要显示的消息内容</param>
        public static void Show(Page page, string message)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), System.Guid.NewGuid().ToString(), "alert(\"" + message.Replace("\n", "").Replace("\\", "\\\\").Replace("\r", "") + "\");", true);
        }
        /// <summary>
        /// 提示消息，关闭当地窗口并刷新父窗口
        /// </summary>
        /// <param name="control">要注册到的控件</param>
        /// <param name="message">需要显示的消息内容</param>
        public static void Show(Page page, string message,bool ret)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), System.Guid.NewGuid().ToString(), "alert(\"" + message.Replace("\n", "").Replace("\\", "\\\\").Replace("\r", "") + "\");window.close();opener.location.href=opener.location.href.replace(/#/g,'');", true);
        }
        /// <summary>
        /// 显示窗口
        /// </summary>
        /// <param name="control">要注册到的控件</param>
        /// <param name="title">窗口标题</param>
        /// <param name="url">窗口地址</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        public static void Show(Page page, string title, string url, int width, int height)
        {
            string script = @"window.open('" + url + @"','" + title + @"','width=" + width.ToString() + @",height=" + height.ToString() + @",menubar=no,resizable=no');";
            page.ClientScript.RegisterStartupScript(page.GetType(), System.Guid.NewGuid().ToString(), script, true);
        }
        /// <summary>
        /// 弹出消息框并跳转
        /// </summary>
        /// <param name="control">要注册到的控件</param>
        /// <param name="message">消息内容</param>
        /// <param name="redirectUrl">跳转的页面</param>
        /// <param name="isTop">是否显示在顶部，对于嵌入Iframe的页面跳转时可以通过设置为true，条转出Iframe</param>
        public static void ShowAndRedirect(Page page, string message, string redirectUrl, bool isTop)
        {
            if (isTop)
            {
                page.ClientScript.RegisterClientScriptBlock(page.GetType(), System.Guid.NewGuid().ToString(), "alert('" + message.Replace("\n", "").Replace("\\", "\\\\").Replace("\r", "") + "');top.location.href='" + redirectUrl + "';", true);
            }
            else
            {
                page.ClientScript.RegisterClientScriptBlock(page.GetType(), System.Guid.NewGuid().ToString(), "alert('" + message.Replace("\n", "").Replace("\\", "\\\\").Replace("\r", "") + "');window.location='" + redirectUrl + "';", true);
            }
        }
    }
}
